<?
include_once('arc/ARC2.php');
header('Content-Type: text/plain');
$c = $_POST['j'];
//echo $c;exit(0);
$json=json_decode($c, true);
//print_r($json);
$nodes = array();

foreach($json as $k => $v){
  if($v['type'] == 'resource'){
    $nodes[$v['resource']['shape']['uuid']]['t'] = 'resource';
    $nodes[$v['resource']['shape']['uuid']]['type'] = $v['rdftype'];
    $nodes[$v['resource']['shape']['uuid']]['uri'] = $v['resource']['element']['name'];
  }elseif($v['type'] == 'literal'){
    $nodes[$v['shape']['uuid']]['t'] = 'literal';
    $nodes[$v['shape']['uuid']]['uri'] = $v['text'];
    $nodes[$v['shape']['uuid']]['datatype'] = $v['datatype'];
    $nodes[$v['shape']['uuid']]['lang'] = $v['lang'];
  }
}

$triples = array();
foreach($json as $k => $v){    
  //echo $nodes[$v['uuidIni']]['t'] ."\n";
  if($v['type'] == 'property'){
    $aux = array();
    $auxtypes = array();
    $auxtypeo = array();
    $aux['s']= $nodes[$v['uuidIni']]['uri'];
    if(substr($aux['s'], 0, 2) == "_:"){
      $aux['s_type'] = 'bnode';
    }else{
      $aux['s_type'] = 'uri';
    }
    if($nodes[$v['uuidIni']]['type']){
      $auxtypes['s'] = $aux['s'];
      $auxtypes['s_type'] = $aux['s_type'];
      $auxtypes['p'] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type";
      $auxtypes['o'] = $nodes[$v['uuidIni']]['type'];
      array_push($triples, $auxtypes);
    }
    $aux['p'] = $v['resource']['element']['namespace']['uri'];
    
    if($nodes[$v['uuidEnd']]['t'] == 'resource'){
      $aux['o']= $nodes[$v['uuidEnd']]['uri'];
      if(substr($aux['o'], 0, 2) == "_:"){
        $aux['o_type'] = 'bnode';
      }else{
        $aux['o_type'] = 'uri';
      }
    }elseif($nodes[$v['uuidEnd']]['t'] == 'literal'){
      $aux['o'] = $nodes[$v['uuidEnd']]['uri'];
      $aux['o_datatype'] = $nodes[$v['uuidEnd']]['datatype'];
      $aux['o_lang'] = $nodes[$v['uuidEnd']]['lang'];
    }else{
      echo 'ERROR';
      echo $c;
      exit(1);
    }
    array_push($triples, $aux);
    
  }
}
$conf = array();
$conf['serializer_type_nodes'] = true;
$conf['serializer_default_ns'] = 'http://inference-web.org/2.0/pml-provenance.owl#';
$conf['ns'] = array("" => 'http://inference-web.org/2.0/pml-provenance.owl#', 'pmlj' => 'http://inference-web.org/2.0/pml-justification.owl#', 'pmlt' => 'http://inference-web.org/2.0/pml-trust.owl#', 'rdf'=>'http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'ds' => 'http://inference-web.org/2.0/ds.owl#', 'xsd' => 'http://www.w3.org/2001/XMLSchema#');


$ser = ARC2::getRDFXMLSerializer($conf);
echo $ser->getSerializedTriples($triples);
//print_r($triples);
?>
